package control;

import gui.uidtoui.OptionGUI;

import java.io.File;
import java.util.List;

import xml.ConverterToUidXml;
import MDDfromCIMtoPIM.files.CIM2PIM;
import MDDfromPIMtoPSM.files.PIM2PSM;

public class Run {

	public static void main(String[] args) {

		// FlowNavigationManagement flowNavigationManagement = new
		// FlowNavigationManagement(
		// null);
		// GingaHyperDTO gingaHyperDTO = new GingaHyperDTO();
		// flowNavigationManagement.doNewAction(gingaHyperDTO, null, null);

		try {
			teste();
		} catch (Exception e) {
			System.out.println("Natan");
			e.printStackTrace();
		}

		// run();

	}

	private static void teste() {

		OptionGUI managedGUI = null;
		if (managedGUI == null || managedGUI.getShell().isDisposed()) {
			managedGUI = new OptionGUI(null);
			managedGUI.mountLayout();
			managedGUI.show();
		}

		while (!managedGUI.getShell().isDisposed()) {
			if (!managedGUI.getShell().getDisplay().readAndDispatch()) {
				managedGUI.getShell().getDisplay().sleep();
			}
		}
	}

	public static void run(String directory, List<String> names) {
		long startTime = System.currentTimeMillis();
		System.out.println(">>>>>> Start MDDRun");

		// String[] names = { "BuscarHoteis", "CadastrarCliente", "FazerLogin",
		// "FazerReserva", "VisualizarHotel", "VisualizarReserva",
		// "VisualizarReservas", "Completo"};
		// String[] names = { "SearchHotels", "MakeReservation", "ViewHotel"};
		// String[] names = { "MakeReservation" };
		// String[] names = { "FazerReserva" };
		// String[] names = { "TaskList", "TaskAdd", "TaskEdit" };

		String baseDir = "\\Desenvolvimento\\Workspace\\WSEclipse";
		String projectUIDDir = baseDir + "\\UID";
		String projectMDDDir = baseDir + "\\MDDfromUIDtoUI\\files";
		String projectJsfDir = baseDir + "\\JavaServerFaces";
		String projectAspDir = "\\Desenvolvimento\\Workspace\\wsVisualStudio\\AspDotNet";

		String psmEcoreDir = projectMDDDir + "\\PSM.ecore";

		String psmbyaspEcoreDir = projectMDDDir + "\\PSMbyASP.ecore";

		limparProjetos(projectMDDDir);
		limparProjetos(projectJsfDir);
		limparProjetos(projectAspDir);

		for (String name : names) {

			System.out.println(">>>" + name);

			String xuidDir = projectUIDDir + "\\src\\" + directory + "\\"
					+ name + ".xuid";

			String xuiDirBKP = projectMDDDir + "\\" + name + ".xuid";
			String cimDir = projectMDDDir + "\\cim" + name + ".xmi";
			String pimDir = projectMDDDir + "\\pim" + name + ".xmi";

			String psmDir = projectMDDDir + "\\psm" + name + ".xmi";

			String psmbyaspDir = projectMDDDir + "\\psmbyasp" + name + ".xmi";

			String[] converterToUidXmlArgs = { xuidDir, cimDir, xuiDirBKP };
			ConverterToUidXml.main(converterToUidXmlArgs);

			String[] cim2pimArgs = { cimDir, pimDir };
			CIM2PIM.main(cim2pimArgs);

			String[] pim2psmArgs = { pimDir, psmDir };
			PIM2PSM.main(pim2psmArgs);

			String[] psm2codeArgs = { psmDir, projectJsfDir, psmEcoreDir };
			JavaServerFacesAcceleo.main.Main.main(psm2codeArgs);

			// String[] pim2psmbyaspArgs = { pimDir, psmbyaspDir };
			// PIM2PSMbyASP.main(pim2psmbyaspArgs);

			// String[] psmbyasp2codeArgs = { psmbyaspDir, projectAspDir,
			// psmbyaspEcoreDir };
			// AspDotNetAcceleo.main.Main.main(psmbyasp2codeArgs);

		}

		long endTime = System.currentTimeMillis();

		System.out.println(">>>>>> End MDDRun in "
				+ ((float) (endTime - startTime) / 1000) + "s");
	}

	private static void limparProjetos(String projectDir) {
		File projectDirFile = new File(projectDir);
		for (File file : projectDirFile.listFiles()) {
			if (file.getName().endsWith(".xhtml")
					|| file.getName().endsWith(".aspx")
					|| file.getName().endsWith(".xml")
					|| file.getName().endsWith(".MF")
//					|| file.getName().endsWith(".java")
					|| file.getName().endsWith(".cs")
					|| file.getName().endsWith(".config")
					|| file.getName().endsWith(".xmi")) {
				file.delete();
			}
			if (file.isDirectory()) {
				limparProjetos(file.getAbsolutePath());
			}
		}
	}

}